from rest_framework import viewsets, status
from rest_framework.response import Response
from .models import ManipulateConcept, ManipulateEssay
from .serializers import ManipulateConceptSerializer, ManipulateEssaySerializer

class ManipulateConceptViewSet(viewsets.ModelViewSet):
    """
    ViewSet for ManipulateConcept model
    """
    queryset = ManipulateConcept.objects.all()
    serializer_class = ManipulateConceptSerializer

    def create(self, request, *args, **kwargs):
        print('ssss',request.data)
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    
class ManipulateEssayViewSet(viewsets.ModelViewSet):
    """
    ViewSet for ManipulateEssay model
    """
    queryset = ManipulateEssay.objects.all()
    serializer_class = ManipulateEssaySerializer
    
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        
    def list(self, request, *args, **kwargs):
        """重写list方法，返回所有数据，不使用分页"""
        queryset = self.get_queryset()
        print(f'{request.query_params}')
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)